﻿using PickingGuide.Handle;
using PickingGuide.Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Runtime.CompilerServices.RuntimeHelpers;

namespace PickingGuideBoard
{
    public partial class Form1 : Form
    {
        PickingGuideHandler pickingGuideHandler;
        int QueueLength;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pickingGuideHandler = PickingGuideHandler.getInstance();
            var systemConfig = pickingGuideHandler.GetSystemConfig();
            //QueueLength = systemConfig.QueueLength;
            QueueLength = 5;
            //dgvQueue.ClearSelection();
            //dgvQueue.AutoGenerateColumns = false;
            //dgvQueue.ReadOnly = true;
            //dgvQueue.AddColumn("连番", "Sequence", 15);
            //dgvQueue.AddColumn("车身码", "BodyNum", 20);
            //dgvQueue.AddColumn("VIN", "VIN", 45);
            //dgvQueue.AddColumn("车型", "CarModel", 25);
            ////dgvQueue.AddColumn("接收时间", "CreatedDate", 40);
            //
            //var rowHeight = dgvQueue.Height / (QueueLength + 1);
            //
            //dgvQueue.RowHeight = rowHeight;
            //dgvQueue.ColumnHeadersHeight = rowHeight;

            //设置Timer1控件可用
            this.timer1.Enabled = false;
            //设置Timer1 时间间隔
            this.timer1.Interval = 500;
            this.timer1.Start();
        }

        private void WorkGuideRefresh(QueueInfoModel model)
        {
            this.Invoke(new MethodInvoker(() =>
            {
                //txtVIN.Text = model.VIN;
                //txtBody.Text = model.BodyNum;
                //txtSequence.Text = model.Sequence.ToString("000");
                uiPanelSequence.Text = model.Sequence.ToString("000");
                uiPanelMark.Text = pickingGuideHandler.GetMark(model);
                uiPanelModel.Text = model.CarModel;
            }));
        }

        public void DgvRefresh(List<QueueInfoModel> queue)
        {
            this.Invoke(new MethodInvoker(() =>
            {
                if (queue.Count < 1)
                    return;
                uiPanel01.Text = queue[0].Sequence.ToString("000");
                if (queue[0].Status == "ok")
                {
                    uiPanel01.BackColor = Color.LightGreen;
                    uiPanel01.FillColor = Color.LightGreen;
                }
                else if (queue[0].Status == "work")
                {
                    uiPanel01.BackColor = Color.Yellow;
                    uiPanel01.FillColor = Color.Yellow;
                }
                else if (queue[0].Status == "unok")
                {
                    uiPanel01.BackColor = Color.Red;
                    uiPanel01.FillColor = Color.Red;
                }
                else if (queue[0].Status == "pass")
                {
                    uiPanel01.BackColor = Color.LightGray;
                    uiPanel01.FillColor = Color.LightGray;
                }
                else
                {
                    uiPanel01.BackColor = Color.White;
                    uiPanel01.FillColor = Color.White;
                }
                uiPanel01.Refresh();

                if (queue.Count < 2)
                    return;
                uiPanel02.Text = queue[1].Sequence.ToString("000");
                if (queue[1].Status == "ok")
                {
                    uiPanel02.BackColor = Color.LightGreen;
                    uiPanel02.FillColor = Color.LightGreen;
                }
                else if (queue[1].Status == "work")
                {
                    uiPanel02.BackColor = Color.Yellow;
                    uiPanel02.FillColor = Color.Yellow;
                }
                else if (queue[1].Status == "unok")
                {
                    uiPanel02.BackColor = Color.Red;
                    uiPanel02.FillColor = Color.Red;
                }
                else if (queue[1].Status == "pass")
                {
                    uiPanel02.BackColor = Color.LightGray;
                    uiPanel02.FillColor = Color.LightGray;
                }
                else
                {
                    uiPanel02.BackColor = Color.White;
                    uiPanel02.FillColor = Color.White;
                }
                uiPanel02.Refresh();

                if (queue.Count < 3)
                    return;
                uiPanel03.Text = queue[2].Sequence.ToString("000");
                if (queue[2].Status == "ok")
                {
                    uiPanel03.BackColor = Color.LightGreen;
                    uiPanel03.FillColor = Color.LightGreen;
                }
                else if (queue[2].Status == "work")
                {
                    uiPanel03.BackColor = Color.Yellow;
                    uiPanel03.FillColor = Color.Yellow;
                }
                else if (queue[2].Status == "unok")
                {
                    uiPanel03.BackColor = Color.Red;
                    uiPanel03.FillColor = Color.Red;
                }
                else if (queue[2].Status == "pass")
                {
                    uiPanel03.BackColor = Color.LightGray;
                    uiPanel03.FillColor = Color.LightGray;
                }
                else
                {
                    uiPanel03.BackColor = Color.White;
                    uiPanel03.FillColor = Color.White;
                }
                uiPanel03.Refresh();

                if (queue.Count < 4)
                    return;
                uiPanel04.Text = queue[3].Sequence.ToString("000");
                if (queue[3].Status == "ok")
                {
                    uiPanel04.BackColor = Color.LightGreen;
                    uiPanel04.FillColor = Color.LightGreen;
                }
                else if (queue[3].Status == "work")
                {
                    uiPanel04.BackColor = Color.Yellow;
                    uiPanel04.FillColor = Color.Yellow;
                }
                else if (queue[3].Status == "unok")
                {
                    uiPanel04.BackColor = Color.Red;
                    uiPanel04.FillColor = Color.Red;
                }
                else if (queue[3].Status == "pass")
                {
                    uiPanel04.BackColor = Color.LightGray;
                    uiPanel04.FillColor = Color.LightGray;
                }
                else
                {
                    uiPanel04.BackColor = Color.White;
                    uiPanel04.FillColor = Color.White;
                }
                uiPanel04.Refresh();

                if (queue.Count < 5)
                    return;
                uiPanel05.Text = queue[4].Sequence.ToString("000");
                if (queue[4].Status == "ok")
                {
                    uiPanel05.BackColor = Color.LightGreen;
                    uiPanel05.FillColor = Color.LightGreen;
                }
                else if (queue[4].Status == "work")
                {
                    uiPanel05.BackColor = Color.Yellow;
                    uiPanel05.FillColor = Color.Yellow;
                }
                else if (queue[4].Status == "unok")
                {
                    uiPanel05.BackColor = Color.Red;
                    uiPanel05.FillColor = Color.Red;
                }
                else if (queue[4].Status == "pass")
                {
                    uiPanel05.BackColor = Color.LightGray;
                    uiPanel05.FillColor = Color.LightGray;
                }
                else
                {
                    uiPanel05.BackColor = Color.White;
                    uiPanel05.FillColor = Color.White;
                }
                uiPanel05.Refresh();
            }));
        }

        public void Dgv_Cells(ref UIDataGridView dgv)
        {
            if (dgv.Rows.Count > 0)
                dgv.Rows[0].Selected = false;

            foreach (DataGridViewRow item in dgv.Rows)
            {
                var model = item.DataBoundItem as QueueInfoModel;
                if (model.Status == "ok")
                {
                    item.DefaultCellStyle.BackColor = Color.LightGreen;
                }
                else if (model.Status == "work")
                {
                    item.DefaultCellStyle.BackColor = Color.Yellow;
                }
                else if (model.Status == "unok")
                {
                    item.DefaultCellStyle.BackColor = Color.Red;
                }
                else if (model.Status == "pass")
                {
                    item.DefaultCellStyle.BackColor = Color.LightGray;
                }
                else
                {
                    item.DefaultCellStyle.BackColor = Color.White;
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            var list = pickingGuideHandler.GetQueueBoard(QueueLength);
            if (list != null && list.Count > 0)
                DgvRefresh(list);
            var model = pickingGuideHandler.GetWorkModel();
            if (model != null)
                WorkGuideRefresh(model);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.timer1.Stop();
            this.Close();
        }
    }
}
